#!/usr/bin/env python
# encoding: utf-8
'''
@author: 王震
@license: 四川凡狼科技有限公司
@contact: 2668985108@qq.com
@file: classHour.py
@time: 2021/10/1 11:10 下午
@desc:
'''

from app.model.classHour.classHour import ClassHour
from app.model.curriculum.curriculum import NewCourse
from app.serialize.newCourseList import NewCourseList as CourseList
from app.msg.tips import tipsText, tips
from app.msg.resp import response_json, response_list_json
from app.public.sqlMethods import get_obj, existence
from main import db
from app.public.tool import create_key
from app.serialize.classHour import ClassHour as ClassHourObj
from app.public.sqlMethods import auto_commit_db
from sqlalchemy import and_


def add_class_hours(_dict):
    obj = {
        'name': '',
        'parentId': ""
    }
    if existence(obj=_dict, target=obj):
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    data = NewCourse.query.filter_by(id=_dict['parentId']).first()
    if not data:
        return response_json(code=tips['no_curriculum'], msg=tipsText['no_curriculum'])
    res = ClassHour.query.filter(ClassHour.name == _dict['name']).first()
    if res:
        return response_json(code=tips['existence'], msg=tipsText['existence'])
    class_hour = ClassHour()
    for key in _dict:
        setattr(class_hour, key, _dict[key]) if hasattr(class_hour, key) and _dict[key] else None
    class_hour.id = create_key()
    with auto_commit_db():
        db.session.add(class_hour)
    return response_json()


def get_class_hour(page_number, page_size, parent_id):
    if not parent_id:
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    with auto_commit_db():
        _a = and_(ClassHour.parentId == parent_id, ClassHour.show == 1)
        pages = ClassHour.query.filter(_a).paginate(page_number, page_size, False)
        item = pages.items
        class_hour = ClassHourObj(many=True)
        data = class_hour.dump(item)
    return response_list_json(data=data, page_number=pages.page, page_size=pages.pages)


def system_get_class_hour(page_number, page_size):
    with auto_commit_db():
        pages = ClassHour.query.paginate(int(page_number), int(page_size), False)
        item = pages.items
        class_hour = ClassHourObj(many=True)
        data = class_hour.dump(item)
        _courseList = CourseList()
        for item in data:
            course = NewCourse.query.filter_by(id=item['parentId']).first()
            item_data = _courseList.dump(course)
            item['curriculum'] = item_data
    return response_list_json(data=data, page_number=pages.page, page_size=pages.pages)


def update_show(data):
    obj = {'id': '', 'show': ''}
    if existence(obj=data, target=obj):
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    with auto_commit_db():
        res = ClassHour.query.filter_by(id=data['id']).update({'show': int(data['show'])})
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
    return response_json()


def get_shoe(page_number, page_size, show, _id):
    with auto_commit_db():
        if not _id:
            pages = ClassHour.query.filter(ClassHour.show == int(show)).paginate(
                int(page_number), int(page_size), False)
        else:
            pages = ClassHour.query.filter(and_(ClassHour.parentId == _id, ClassHour.show == int(show))).paginate(
                int(page_number), int(page_size), False)
        item = pages.items
        new_course = ClassHourObj(many=True)
        data = new_course.dump(item)
    return response_list_json(data=data, page_number=pages.page, page_size=pages.pages)


def update_class_hour(data):
    obj = {
        'name': "",
        "id": "",
        'parentId': ''
    }
    if existence(obj=data, target=obj):
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    if get_obj(name=data['name'], class_obj=ClassHour):
        return response_json(code=tips['existence'], msg=tipsText['existence'] % data['name'])
    with auto_commit_db():
        res = ClassHour.query.filter_by(id=data['id']).update(data)
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
    return response_json()


def delete_class_hour(_id):
    if not _id:
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    with auto_commit_db():
        res = ClassHour.query.filter_by(id=_id).delete()
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
    return response_json()


def customer_class_list(parent_id):
    obj = {
        'parentId': ""
    }
